﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using WebGioiThieu.Models;

namespace WebGioiThieu.Areas.Admin.Controllers
{
    public class AccountController : Controller
    {
        public IFormsAuthenticationService FormsService { get; set; }
        public IMembershipService MembershipService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            if (MembershipService == null) { MembershipService = new AccountMembershipService(); }

            base.Initialize(requestContext);
        }


        public ActionResult LogOn()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (MembershipService.ValidateUser(model.UserName, model.Password))
                {
                    FormsService.SignIn(model.UserName, model.RememberMe);
                    if (Url.IsLocalUrl(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "System");
                    }
                }
                else
                {
                    ModelState.AddModelError("", " Tên đăng nhập hoặc mật khẩu không chính xác.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        public ActionResult LogOff()
        {
            FormsService.SignOut();
            Session["Permissions"] = null;
            return RedirectToAction("Index", "System");
        }
        [Authorize]
        public ActionResult ChangePassword()
        {
            ViewBag.PasswordLength = MembershipService.MinPasswordLength;
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult ChangePassword(ChangePasswordModel model)
        {
            if (ModelState.IsValid)
            {
                if (MembershipService.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword))
                {
                    return RedirectToAction("ChangePasswordSuccess");
                }
                else
                {
                    ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
                }
            }

            // If we got this far, something failed, redisplay form
            ViewBag.PasswordLength = MembershipService.MinPasswordLength;
            return View(model);
        }

        public ActionResult ChangePasswordSuccess()
        {
            return View();
        }

        private IntroductionWebDBEntities db = new IntroductionWebDBEntities();

    
        public ActionResult Edit()
        {
            var user = db.aspnet_Users.Single(u => u.UserName == User.Identity.Name);
            aspnet_Membership member = db.aspnet_Membership.Single(m => m.UserId.CompareTo(((aspnet_Users)user).UserId)==0);
            return View(member);
        }
        [HttpPost,ActionName("Edit")]
        public ActionResult Edit(aspnet_Membership model)
        {
            var user = db.aspnet_Users.Single(u => u.UserName == User.Identity.Name);
            aspnet_Membership member = db.aspnet_Membership.Single(m => m.UserId.CompareTo(((aspnet_Users)user).UserId) == 0);
            if (ModelState.IsValid) {
                member.FullName = model.FullName;
                member.Gender = model.Gender;
                member.Address = model.Address;
                member.Birthday = model.Birthday;
                member.PhoneNumber = model.PhoneNumber;
                member.IdentityCard = model.IdentityCard;
                member.Email = model.Email;
                db.Entry(member).State = System.Data.EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index","System");
            }
            return View();
        }
    
        
    }

}
